﻿@{
    ViewBag.Title = "卡号管理";
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}
<style type="text/css">
    .layout-split-west {
        border-right-color: Window;
    }
</style>

<div class="easyui-layout" data-options="fit:true" scroll="no">
    <div data-options="region:'west',split:true,title:'卡号类别',iconCls: 'icon-org'" style="width:210px">
        <ul id="tgCPType"></ul>
    </div>
    <div data-options="region:'center',noheader:true,border:false">
        <table id="dgCard"></table>
    </div>
</div>
<div id="tbar">
    <div class="tbar">
        <div class="z-toolbar">
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-arrow_refresh',plain:true" onclick="ToolbarRefresh();">刷新</a>
            <div class="datagrid-btn-separator"></div>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="ToolbarCreateCard();">生成卡号</a>
            <div class="datagrid-btn-separator"></div>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="ToolbarJiami();">加密</a>
            <div class="datagrid-btn-separator"></div>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" onclick="ToolbarEdit();">编辑</a>
            <div class="datagrid-btn-separator"></div>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cross',plain:true" onclick="ToolbarRemove();">删除</a>
            <div class="datagrid-btn-separator"></div>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="ToolbarUnselect();">取消选中</a>
        </div>
        <form id="searchForm">
            <table>
                <tr>
                    <td>卡号:</td>
                    <td class="td-left"><input type="text" name="CPName" class="easyui-textbox" /></td>
                    <td class="td-right"><a id="btnSearch" href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="btnSearch_OnClick()">查询</a></td>
                    <td class="td-right"><a id="btnSearch" href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-undo'" onclick="cancelSearch()">取消</a></td>
                </tr>
            </table>
        </form>
    </div>
</div>
@section PageSpecificScript{
    <script type="text/javascript">
        var dgCard;/*显示卡号*/
        var ctypeTree;
        var cpStatusData;
        var searchForm;
        $(document).ready(function () {
            cpStatusData = [
             {
                 value: 0,
                 text: '生成'
             },
             {
                 value: 1,
                 text: '激活'
             },
             {
                 value: 2,
                 text: '已预约'
             },
             {
                 value: 3,
                 text: '已核销'
             },
             {
                 value: 4,
                 text: '已作废'
             }];
            searchForm = $("#searchForm");
            $.validateUrl("/Admin/Sys_Dict/GetTreeDictValue?keyId=3", function () {
                ctypeTree = $("#tgCPType").tree({
                    url: '/Admin/Sys_Dict/GetTreeDictValue?keyId=3',
                    onLoadSuccess: function (node, data) {
                        if (data.Msg) {
                            xz.dealWith(data);
                        }
                    },
                    onClick: function (node) {
                        dgCard.datagrid('load', {
                            CPTId: node.id
                        });
                    },
                    onDblClick: function (node) {
                        if (node.state = "closed") {
                            $(this).tree("expand", node.target);
                        } else {
                            $(this).tree("collapse", node.target);
                        }
                    }
                });
                loadGrid();
            });
        }, xz.dealWith, 'GET');
        /*加载datagrid*/
        function loadGrid() {
            dgCard = $("#dgCard").datagrid({
                url: '/Admin/BS_CardPool/GetCardPoolByCondition',
                method: 'Post',
                idField: 'CPId',
                fit: true,
                fitColumns: true,
                rownumbers: true,
                striped: true,
                pagination: true,
                singleSelect: true,
                toolbar: '#tbar',
                pageSize: 20,
                pageList: [20, 30, 40, 50],
                frozenColumns: [[
                    { field: 'CPId', title: 'CPId', hidden: true },
                    { field: 'CPTId', title: 'CPTId', hidden: true }
                ]],
                columns: [[
                    { field: 'CPTName', title: '卡类别', width: 60, halign: 'center', align: 'center' },
                    { field: 'CPName', title: '卡号', width: 90, halign: 'center', align: 'center' },
                    { field: 'CPPassword', title: '密码', width: 80, halign: 'center', align: 'center' },
                    {
                        field: 'CPStatus', title: '状态', width: 60, halign: 'center', align: 'center', formatter: function (value, row, index) {
                            if (value == 0) {
                                return "生成";
                            } else if (value == 1) {
                                return "激活";
                            } else if (value == 2) {
                                return "已预约";
                            } else if (value == 3) {
                                return "已核销";
                            } else if (value == 4) {
                                return "已作废";
                            }
                        }
                    },
                    { field: 'CPValidityNum', title: '有效次数', width: 50, halign: 'center', align: 'center' },
                    { field: 'CPUsedNum', title: '已用次数', width: 80, halign: 'center', align: 'center' },
                    { field: 'CPMedicalPackageCode', title: '套餐编码', width: 80, halign: 'center', align: 'center' },
                    {
                        field: 'CPValidityTime', title: '有效日期', width: 70, halign: 'center', align: 'center', formatter: function (value, row, index) {
                            if (value) {
                                return eval("new " + value.substr(1, value.length - 2)).toLocaleDateString();
                            }
                        }
                    }
                ]]
            });
        }
        /*过滤卡号信息*/
        function btnSearch_OnClick() {
            dgCard.datagrid('load', xz.serializeObject(searchForm));
        }
        /*取消过滤卡号信息*/
        function cancelSearch() {
            searchForm.form('clear');
            dgCard.datagrid('load', {});
        }
        /**刷新菜单**/
        function ToolbarRefresh() {
            dgCard.datagrid("load");
        }
        /**增加套餐卡**/
        function ToolbarCreateCard() {
            showAppendCardView();
        }
        /**编辑菜单**/
        function ToolbarEdit() {
            showEditView();
        }
        /**批量加密**/
        function ToolbarJiami() {
            showJiami();
        }
        /**删除菜单**/
        function ToolbarRemove() {
            removeCodePool();
        }
        /**取消选中菜单**/
        function ToolbarUnselect() {
            dgCard.datagrid("unselectAll");
        }
        /*打开生成卡号的页面*/
        function showAppendCardView() {
            $.validateUrl('/Admin/BS_CardPool/EditCardView', appendPackage, xz.dealWith, 'GET');
        }
        /*加密卡号*/
        function showJiami() {
            $.ajax({
                type: 'POST',
                url: '/Admin/BS_CardPool/JiamiCardPwd',
                cache: false,
                dataType: 'JSON',
                success: function (jsonData) {
                    $.procAjaxMsg(jsonData, function () {
                        dgCard.datagrid("reload");
                        $.alertMsg(jsonData.Msg, "操作提示", null);
                    }, function () {
                        $.alertMsg(jsonData.Msg, "操作提示", null);
                    });
                }
            });
        }
        /*生成卡号*/
        function appendPackage() {
            var pAdd = xz.dialog({
                title: '生成卡号',
                width: 680,
                height: 400,
                iconCls: 'icon-add',
                href: '/Admin/BS_CardPool/EditCardView',
                buttons: [{
                    text: '生成',
                    iconCls: 'icon-add',
                    handler: function () {
                        var f = pAdd.find('form');
                        f.form('submit', {
                            url: '/Admin/BS_CardPool/CreateCardPool',
                            onSubmit: function () {
                                return true;
                            },
                            success: function (jsonData) {
                                jsonData = $.parseJSON(jsonData);
                                $.procAjaxMsg(jsonData, function () {
                                    pAdd.dialog('close');
                                    dgCard.datagrid("reload");
                                    $.alertMsg(jsonData.Msg, "操作提示", null);
                                }, function () {
                                    $.alertMsg(jsonData.Msg, "操作提示", null);
                                });
                            }
                        });
                    }
                }, {
                    text: '关闭',
                    iconCls: 'icon-cancel',
                    handler: function () {
                        pAdd.dialog('close');
                    }
                }],
                onLoad: function () {
                    var f = pAdd.find('form');
                    var cpTId = f.find('input[name=CPTId]');
                    var cpBranchId = f.find('input[name=CPBranchId]');
                    var cpMedicalPackageId = f.find('input[name=CPMedicalPackageId]');

                    window.setTimeout(function () {
                        cpTId.focus();
                    }, 100);

                    cpTId.combobox({
                        url: '/Admin/Sys_Dict/GetDictValueByKey?keyId=3',
                        method: 'GET',
                        valueField:'id',
                        textField:'text',
                        required: true
                    });

                    cpBranchId.combotree({
                        url: '/Admin/UO_Organization/GetTreeOrgInfo',
                        method: 'GET',
                        required: true,
                        lines: true,
                        multiple: true,
                        cascadeCheck: false,
                        valueField: 'id',
                        textField: 'text',
                        editable: false
                    });

                    cpMedicalPackageId.combogrid({
                        url: '/Admin/BS_MedicalPackage/GetMedicalPackageGrid',
                        loadMsg: '数据加载中......',
                        required: true,
                        panelWidth: 510,
                        panelHeight: 180,
                        fitColumns: true,
                        multiple: true,
                        singleSelect: true,
                        idField: 'MPId',
                        textField: 'MPName',
                        method: 'Get',
                        mode: 'remote',
                        pagination: true,
                        pageSize: 5,
                        pageList: [5, 10],
                        columns: [[
                            { field: 'MPId', title: 'MPId', checkbox: true },
                            { field: 'MPName', title: '套餐名称', width: 240 },
                            { field: 'MPPrice', title: '套餐价格', width: 100 },
                            {
                                field: 'MPSex', title: '适用性别', width: 80, halign: 'center', align: 'center', formatter: function (value, row, index) {
                                    if (value == 0) {
                                        return "女";
                                    } else if (value == 1) {
                                        return "男";
                                    } else if (value == 2) {
                                        return "不限";
                                    }
                                }
                            },
                        {
                            field: 'MPMarried', title: '婚姻状况', width: 80, halign: 'center', align: 'center', formatter: function (value, row, index) {
                                if (value == 0) {
                                    return "未婚";
                                } else if (value == 1) {
                                    return "已婚";
                                } else if (value == -1) {
                                    return "不限";
                                }
                            }
                        },
                        ]]
                    });
                }
            })
        }
        
        /*编辑码号*/
        function showEditView() {
            $.validateUrl('/Admin/BS_CodePool/EditCoolPoolView', edit, xz.dealWith, 'GET');
        }
        /*编辑码号*/
        function edit() {
            var node = dgCard.datagrid("getSelected");
            if (node) {
                var pAdd = xz.dialog({
                    title: '修改码号',
                    width: 680,
                    height: 400,
                    iconCls: 'icon-edit',
                    href: '/Admin/BS_CodePool/EditCoolPoolView',
                    buttons: [{
                        text: '修改',
                        iconCls: 'icon-edit',
                        handler: function () {
                            var f = pAdd.find('form');
                            f.form('submit',{
                                url: '/Admin/BS_CodePool/EditCoolPool',
                                success: function (jsonData) {
                                    jsonData = $.parseJSON(jsonData);
                                    $.procAjaxMsg(jsonData, function () {
                                        pAdd.dialog('close');
                                        dgCard.datagrid("reload");
                                        $.alertMsg(jsonData.Msg, "操作提示", null);
                                    }, function () {
                                        $.alertMsg(jsonData.Msg, "操作提示", null);
                                    });
                                }
                            });
                        }
                    }, {
                        text: '关闭',
                        iconCls: 'icon-cancel',
                        handler: function () {
                            pAdd.dialog('close');
                        }
                    }],
                    onLoad: function () {
                        var f = pAdd.find('form');
                        var cpId = f.find('input[name=CPId]');
                        var cpName = f.find('input[name=CPName]');
                        var cpStatus = f.find('input[name=CPStatus]');
                        var cpTId = f.find('input[name=CPTId]');
                        var cpBranchId = f.find('input[name=CPBranchId]');
                        var cpMedicalPackageId = f.find('input[name=CPMedicalPackageId]');
                        var CPShowNum = f.find('input[name=CPShowNum]');
                        var cpUseTime = f.find('input[name=CPUseTime]');
                        var cpRemark = f.find('input[name=CPUseTime]');

                        window.setTimeout(function () {
                            cpStatus.focus();
                        }, 100);

                        cpStatus.combobox({
                            data: cpStatusData
                        });

                        cpTId.combobox({
                            url: '/Admin/Sys_Dict/GetDictValueByKey?keyId=2',
                            method: 'GET',
                            valueField: 'id',
                            textField: 'text',
                            required: true
                        });

                        cpBranchId.combotree({
                            url: '/Admin/UO_Organization/GetTreeOrgInfo',
                            method: 'GET',
                            required: true,
                            lines: true,
                            multiple: true,
                            cascadeCheck: false,
                            valueField: 'id',
                            textField: 'text',
                            editable: false
                        });

                        cpMedicalPackageId.combogrid({
                            url: '/Admin/BS_MedicalPackage/GetMedicalPackageGrid',
                            required: true,
                            loadMsg: '数据加载中......',
                            panelWidth: 510,
                            panelHeight: 180,
                            fitColumns: true,
                            multiple: true,
                            singleSelect: false,
                            idField: 'MPId',
                            textField: 'MPName',
                            method: 'Get',
                            mode: 'remote',
                            pagination: true,
                            pageSize: 5,
                            pageList: [5, 10],
                            columns: [[
                                { field: 'MPId', title: 'MPId', checkbox: true },
                                { field: 'MPName', title: '套餐名称', width: 240 },
                                { field: 'MPPrice', title: '套餐价格', width: 100 },
                                {
                                    field: 'MPSex', title: '适用性别', width: 80, halign: 'center', align: 'center', formatter: function (value, row, index) {
                                        if (value == 0) {
                                            return "女";
                                        } else if (value == 1) {
                                            return "男";
                                        } else if (value == 2) {
                                            return "不限";
                                        }
                                    }
                                },
                            {
                                field: 'MPMarried', title: '婚姻状况', width: 80, halign: 'center', align: 'center', formatter: function (value, row, index) {
                                    if (value == 0) {
                                        return "未婚";
                                    } else if (value == 1) {
                                        return "已婚";
                                    } else if (value == -1) {
                                        return "不限";
                                    }
                                }
                            },
                            ]]
                        });

                        f.form('load', node);
                    }
                })
            } else {
                xz.messageAlert("提示", "请选择要修改的码号", "error");
            }
        }

        /*删除用户*/
        function removeCodePool() {
            var rows = dgCard.datagrid("getSelected");
            if (rows) {
                xz.messageConfirm('询问', '你确定要删除【' + rows.CPName + '】吗?', function (b) {
                    if (b) {
                        $.ajax({
                            type: 'POST',
                            url: '/Admin/BS_CodePool/DelCodePool',
                            data: {
                                CPId: rows.CPId
                            },
                            cache: false,
                            dataType: 'JSON',
                            success: function (jsonData) {
                                $.procAjaxMsg(jsonData, function () {
                                    dgCard.datagrid("reload");
                                    $.alertMsg(jsonData.Msg, "操作提示", null);
                                }, function () {
                                    $.alertMsg(jsonData.Msg, "操作提示", null);
                                });
                            }
                        });
                    }
                });
            } else {
                xz.messageAlert("提示", "请选择要删除的码", "error");
            }
        }


        //选择上级节点
        function selectParent(pid) {
            $("[name=OrgID]:checkbox").each(function () {
                if (pid > 0) {
                    var checked = $(this).attr("checked");
                    $("[name=OrgID]:checkbox").each(function () {
                        if (pid == $(this).val() && checked == "checked") {
                            $(this).attr("checked", "checked");
                            selectParent($(this).parent().parent().parent().siblings().eq(0).find("div").text());
                            return false;
                        }
                    })
                }
            })
        }
        //选择下级节点
        function selectChildren(modID, checked) {
            $("[name=OrgID]:checkbox").each(function () {
                var parentID = $(this).parent().parent().parent().siblings().eq(0).find("div").text();
                if (modID == parentID) {
                    if (checked != "checked") {
                        $(this).removeAttr("checked");
                    }
                    selectChildren($(this).val(), $(this).attr("checked"));
                }
            })
        }
    </script>
}
